#include <QFileDialog>
#include <QMessageBox>
#include "MainWindow.h"
#include "ui_MainWindow.h"

/**
 * 构造方法。
 * @param parent    父窗口
 */
MainWindow::MainWindow(QWidget* parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    mGameBoard(this, 0, 0, Const::GAME_BOARD_WIDTH, Const::GAME_BOARD_HEIGHT),
    mGame(mGameBoard.getBitmap(), &mGameBoard),
    mMenuSetBorder(this),
    mMenuSetDifficulty(this) {
    ui->setupUi(this);
    ui->centralWidget->hide();

    // 游戏区域的 Y 坐标要设置为菜单栏的高度（这样游戏区域就在菜单栏的下方了）
    mGameBoard.move(0, ui->menuBar->height() + 1);

    // 设置菜单项
    mMenuSetDifficulty.addAction(ui->actionEasy);
    mMenuSetDifficulty.addAction(ui->actionMedium);
    mMenuSetDifficulty.addAction(ui->actionHard);
    mMenuSetBorder.addAction(ui->actionHasBorder);
    mMenuSetBorder.addAction(ui->actionNoBorder);

    // 连接信号与槽
    QMainWindow::connect(&mMenuSetBorder, SIGNAL(triggered(QAction*)), SLOT(on_actionSetBorder_triggered(QAction*)));
    QMainWindow::connect(&mMenuSetDifficulty, SIGNAL(triggered(QAction*)), SLOT(on_actionSetDifficulty_triggered(QAction*)));
    QMainWindow::connect(&mGame, SIGNAL(UM_GAME_OVER()), SLOT(onGameOver()));
}

/**
 * 析构方法。
 */
MainWindow::~MainWindow() {
    delete ui;
}

/**
 * "图片边框"菜单项的单击响应方法。
 * @param action    菜单项
 */
void MainWindow::on_actionSetBorder_triggered(QAction* action) {
    // 如果要设置的参数与当前游戏参数相同，则直接返回
    bool hasBorder = action == ui->actionHasBorder;
    if (hasBorder == mGame.pictureHasBorder()) {
        return;
    }

    // 设置边框
    mGame.setPictureBorder(hasBorder);
}

/**
 * "难度"菜单项的单击响应方法。
 * @param action    菜单项
 */
void MainWindow::on_actionSetDifficulty_triggered(QAction* action) {
    // 如果要设置的参数与当前游戏参数相同，则直接返回
    Game::Difficulty newDiff = Game::EASY;
    if (action == ui->actionMedium) {
        newDiff = Game::MEDIUM;
    } else if (action == ui->actionHard) {
        newDiff = Game::HARD;
    }
    Game::Difficulty currentDiff = mGame.getDifficulty();
    if (newDiff == currentDiff) {
        return;
    }

    // 弹出询问对话框
    if (mGame.getStatus() != Game::END) {
        QMessageBox::Button clickedbutton = QMessageBox::question(this, " ", tr("IDS_RESET"));
        if (clickedbutton == QMessageBox::No) {
            action->setChecked(false);
            if (currentDiff == Game::Difficulty::EASY) {
                ui->actionEasy->setChecked(true);
            } else if (currentDiff == Game::Difficulty::MEDIUM) {
                ui->actionMedium->setChecked(true);
            } else {
                ui->actionHard->setChecked(true);
            }
            return;
        }
    }

    // 设置难度
    mGame.setDifficulty(newDiff);
}

/**
 * "换图片"菜单项的单击响应方法。
 */
void MainWindow::on_actionSetPicture_triggered() {
    // 弹出询问对话框
    if (mGame.getStatus() != Game::END) {
        QMessageBox::Button clickedbutton = QMessageBox::question(this, " ", tr("IDS_RESET"));
        if (clickedbutton == QMessageBox::No) {
            return;
        }
    }

    // 弹出选择图片对话框
    QFileDialog fileDlg;
    QString fileName = fileDlg.getOpenFileName(this, nullptr,
        QDir::currentPath(), "*.bmp *.gif *.jpg *.png(*.bmp *.gif *.jpg *.png)");
    if (fileName.isEmpty()) {
        return;
    }

    // 设置图片
    mGame.setPicture(fileName.toStdString().c_str());
}

/**
 * "开始"菜单项的单击响应方法。
 */
void MainWindow::on_actionStart_triggered() {
    mGame.start();
}

/**
 * 游戏结束事件的响应方法。
 */
void MainWindow::onGameOver() {
    // 显示胜利提示信息
    QString text = tr("IDS_WON");
    QMessageBox::information(this, " ", text);
}

/**
 * 图片控件的单击响应方法。
 * @param pictureCtrl   用户单击的图片控件
 */
void MainWindow::onPictureCtrlClick(QWidget* pictureCtrl) {
    mGame.movePicture(pictureCtrl);
}
